//
//  MyStatisticGraphicView.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/6.
//  Copyright © 2016年 吴可高. All rights reserved.
#import "MyStatisticGraphicView.h"
#import "BrokenPointValue.h"
#import "BrokenLine.h"
#import "BrokenAnimation.h"




@implementation BrokenShapeLayer
-(void)addAnimationKeyPath:(NSString*)keyPath
                 fromValue:(NSValue*)fromValue
                   toValue:(NSValue*)toValue
                  duration:(NSTimeInterval)timeInterval
                  delegate:(id)delegate;
{
    CABasicAnimation * baseAnimation  = [CABasicAnimation animationWithKeyPath:keyPath];
    baseAnimation.fromValue = fromValue;
    baseAnimation.toValue  = toValue;
    baseAnimation.duration = timeInterval;
    baseAnimation.delegate = delegate;
    baseAnimation.autoreverses = NO;
    baseAnimation.removedOnCompletion = NO;
    baseAnimation.fillMode =  kCAFillModeForwards;
    [self addAnimation:baseAnimation forKey:keyPath];
}
@end


@interface MyStatisticGraphicView ()
@property (strong, nonatomic) BrokenLine * brokenLine;

@end

@implementation MyStatisticGraphicView
- (instancetype)initWithFrame:(CGRect)frame withData:(NSArray*)items
{
    self = [super initWithFrame:frame];
    if (self) {
        _items  = items;
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp
{
    [self getMaxValue:_items];
}
// Get Max Value
- (void)getMaxValue:(NSArray *)dataArray
{
    self.maxXValue = 0;
    self.maxYValue = 0;
    for (BrokenPointValue *value in dataArray) {
        self.maxXValue = self.maxXValue > value.pointX ? self.maxXValue : value.pointX;
        self.maxYValue = self.maxYValue > value.pointY ? self.maxYValue : value.pointY;
    }
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef  contextRef = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contextRef, [UIColor blueColor].CGColor);
    CGContextSetStrokeColorWithColor(contextRef, [UIColor blueColor].CGColor);
    CGContextSetLineCap(contextRef, kCGLineCapRound);
    CGContextSetLineWidth(contextRef, 1.f);
    
    for (int i = 0; i < 1;i++) {
        CGPoint startPoint = CGPointMake(kMargin_Left , kMargin_Top+self.yDistance * self.ySectionNum);
        
        CGPoint endPoint = CGPointMake(kMargin_Left + self.xDistance * (self.xSectionNum + 1), kMargin_Top + self.yDistance * self.ySectionNum);
        // 绘制横轴
        [self horizontalAxesStartPoint:startPoint endPoint:endPoint contextRef:contextRef];
    }
   
    for (int i = 0; i < 1;i++)
    {
        CGPoint startPoint = CGPointMake(kMargin_Left + i * self.xDistance * (self.xSectionNum + 1), kMargin_Top);
        CGPoint endPoint = CGPointMake(kMargin_Left + i * self.xDistance * (self.xSectionNum + 1), kMargin_Top + self.yDistance * self.ySectionNum);
        // 绘制纵轴
        [self verticalAxesStartPoint:startPoint endPoint:endPoint contextRef:contextRef];
    }
    [self drawXSectionNumbersMark:contextRef];
    CGContextStrokePath(contextRef);

}


-(void)horizontalAxesStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint contextRef:(CGContextRef)contextRef
{
    CGContextMoveToPoint(contextRef, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(contextRef, endPoint.x, endPoint.y);
}

-(void)verticalAxesStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint contextRef:(CGContextRef)contextRef
{
    CGContextMoveToPoint(contextRef, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(contextRef, endPoint.x, endPoint.y);
}

-(void)drawXSectionNumbersMark:(CGContextRef)contextRef
{
    for (int i = 1; i < self.xSectionNum; i++)
    {
        CGContextMoveToPoint(contextRef, kMargin_Left + i * self.xDistance, kMargin_Top + self.ySectionNum * self.yDistance - 3*self.marKWidth);
        
        CGContextAddLineToPoint(contextRef, kMargin_Left + i * self.xDistance, kMargin_Top + self.ySectionNum * self.yDistance);
    }
}

- (NSInteger)xSectionNum {
    return (_xSectionNum != 0) ? _xSectionNum : 10;
}

- (NSInteger)ySectionNum {
    return (_ySectionNum != 0) ? _ySectionNum : 5;
}

- (CGFloat)xDistance {
    return (_xDistance != 0) ? _xDistance : 35;
}

- (CGFloat)yDistance {
    return (_yDistance != 0) ? _yDistance : 30;
}

- (CGFloat)marKWidth {
    return (_marKWidth != 0) ? _marKWidth : 2;
}
// Value Convert To Frame
- (CGFloat)getFrameX:(CGFloat)x {
    CGFloat resultX = (kMargin_Left + (self.xSectionNum * self.xDistance) * (x / self.maxXValue));
    return  resultX;
}

- (CGFloat)getFrameY:(CGFloat)y {
    CGFloat resultY = (kMargin_Left + self.ySectionNum * self.yDistance) - ((self.ySectionNum * self.yDistance) * (y / self.maxYValue));
    return resultY;
}

@end

